/**
 * 列表页面混入
 */
import ButtonPopover from '@/components/ButtonPopover'
import CollapseTab from '@/components/CollapseTab'
import ColumnsController from '@/components/ColumnsController'
import DictionarySelect from '@/components/popsoft/DictionarySelect/DictionarySelect'
import DictionaryRadioGroup from '@/components/popsoft/DictionarySelect/DictionaryRadioGroup'
import DictionaryCheckboxGroup from '@/components/popsoft/DictionarySelect/DictionaryCheckboxGroup'
import PInput from '@/components/popsoft/Query/PInput'
import ListButtonRefresh from '@/components/popsoft/Button/ListButtonRefresh'
import ListButtonQuery from '@/components/popsoft/Button/ListButtonQuery'
import ListButtonAdvanceQuery from '@/components/popsoft/Button/ListButtonAdvanceQuery'
import ListButtonAdd from '@/components/popsoft/Button/ListButtonAdd'
import ListButtonRemove from '@/components/popsoft/Button/ListButtonRemove'
import ListButtonDownloadImportTemplate from '@/components/popsoft/Button/ListButtonDownloadImportTemplate'
import ListButtonImport from '@/components/popsoft/Button/ListButtonImport'
import ListButtonExport from '@/components/popsoft/Button/ListButtonExport'
import TableButtonModify from '@/components/popsoft/Button/TableButtonModify'
import TableButtonRemove from '@/components/popsoft/Button/TableButtonRemove'
import TableButtonEnable from '@/components/popsoft/Button/TableButtonEnable'
import TableButtonDisable from '@/components/popsoft/Button/TableButtonDisable'
import TableButtonView from '@/components/popsoft/Button/TableButtonView'
import ListPager from '@/components/popsoft/Page/ListPager'
import OrganizationMultipleSelect from '@/components/popsoft/CommonSelect/Organization/Multiple'

export const listMixin = {
  components: {
    ButtonPopover,
    CollapseTab,
    ColumnsController,
    DictionarySelect,
    DictionaryRadioGroup,
    DictionaryCheckboxGroup,
    PInput,
    ListButtonRefresh,
    ListButtonQuery,
    ListButtonAdvanceQuery,
    ListButtonAdd,
    ListButtonRemove,
    ListButtonDownloadImportTemplate,
    ListButtonImport,
    ListButtonExport,
    TableButtonModify,
    TableButtonRemove,
    TableButtonEnable,
    TableButtonDisable,
    TableButtonView,
    ListPager,
    OrganizationMultipleSelect

  },
  data() {
    return {
      // 加载中
      loading: false,
      // 表格数据
      tableData: [],
      // 当前行
      currentId: this.constant.NO_ITEM_SELECTED,
      // 总页数
      pageTotal: 0,
      // 分页信息
      pageInfo: {
        // 页码
        pageNum: this.constant.DEFAULT_PAGE_NUM,
        // 页码大小
        pageSize: this.constant.DEFAULT_PAGE_SIZE
      },
      // 排序信息
      sortInfo: {
        sort_field: 'orderNo',
        sort_sortType: 'ascending'
      },
      // 复选框选中项
      multipleSelection: [],
      // 是否为树表
      isTreeTable: false,
      // 数据更新是否需要刷新树
      isRefreshTree: false

    }
  },
  computed: {
    showCols() {
      return this.columnList.filter(item => item.show)
    },
    tableKey() {
      const { path } = this.$route
      return `${path}/table`
    }
  },
  mounted() {
    this.loadData()
  },
  methods: {
    // 初始化
    loadData() {
      return new Promise(resolve => {
        this.getTableData().then(() => {
          resolve()
        })
      })
    },
    // 获取表格数据
    getTableData() {
      return new Promise(resolve => {
        this.loading = true
        const params = Object.assign(this.queryCondition, this.pageInfo, this.sortInfo)
        this.api.page(params)
          .then(res => {
            this.tableData = res.data.records
            this.pageTotal = res.data.total
            resolve()
          }).finally(() => {
            this.loading = false
            this.currentId = this.constant.NO_ITEM_SELECTED
          })
      })
    },
    // 处理分页
    handlePage(value) {
      this.pageInfo.pageNum = value
      this.refresh()
    },
    // 处理分页大小
    handlePageSize(value) {
      this.pageInfo.pageSize = value
      this.refresh()
    },
    // 处理排序
    // eslint-disable-next-line no-unused-vars
    handleSortChange({ column, prop, order }) {
      this.sortInfo.sort_field = prop
      this.sortInfo.sort_sortType = order
      this.refresh()
    },
    // 选中行
    handleRowChange(currentRow) {
      this.currentId = currentRow ? currentRow.id : this.constant.NO_ITEM_SELECTED
    },
    // 复选多行
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 双击事件
    handleDoubleClickRow(row) {
      this.$refs.detail.modify(row.id)
    },
    // 处理查询
    query() {
      // 查询之前，将当前页置为1
      this.pageInfo.pageNum = 1
      this.refresh()
    },
    // 新增
    add() {
      this.$refs.detail.add()
    },
    // 移除
    remove(id) {
      this.$confirm('此操作将删除数据, 是否继续?', '确认', {
        type: 'warning'
      }).then(() => {
        this.api.remove(id)
          .then(() => {
            this.loadData()
          })
      }).catch(() => {
        this.$message.info('已取消')
      })
    },
    // 批量移除
    batchRemove() {
      if (!this.checkSelected()) {
        return
      }
      this.$confirm('此操作将批量删除数据, 是否继续?', '确认', {

        type: 'warning'
      }).then(() => {
        const ids = this.getCheckedId()
        this.api.remove(ids)
          .then(() => {
            this.loadData()
          })
      }).catch(() => {
        this.$message.info('已取消')
      })
    },
    // 验证是否有选中行
    checkSelected() {
      if (this.multipleSelection.length === 0) {
        this.$message.info('请至少选中一行')
        return false
      }
      return true
    },
    // 编辑
    modify(id) {
      this.$refs.detail.modify(id)
    },
    // 查看
    view(id) {
      this.$refs.detail.view(id)
    },

    // 刷新
    refresh() {
      this.getTableData()
    },
    // 判断是否选中行 ,尚未使用
    checkRowSelected() {
      if (this.currentId === this.constant.NO_ITEM_SELECTED) {
        this.$message.info('请先选中一行')
        return false
      }
      return true
    },
    // 获取表格选中行id
    getCheckedId() {
      let ids = ''
      this.multipleSelection.forEach(function (item) {
        ids += item.id + ','
      })
      return ids
    },
    // 启用
    enable(id) {
      this.$confirm('此操作将启用该数据, 是否继续?', '确认', {

        type: 'warning'
      }).then(() => {
        this.api.enable(id)
          .then(() => {
            this.refresh()
          })
      }).catch(() => {
        this.$message.info('已取消')
      })
    },
    // 停用
    disable(id) {
      this.$confirm('此操作将停用该数据, 是否继续?', '确认', {

        type: 'warning'
      }).then(() => {
        this.api.disable(id)
          .then(() => {
            this.refresh()
          })
      }).catch(() => {
        this.$message.info('已取消')
      })
    },
    // 下载导入模板
    downloadImportTemplate() {
      this.api.downloadImportTemplate()
    },
    // 导入
    import(file) {
      const formData = new FormData()
      formData.append('file', file)
      this.api.import(formData).finally(() => {
        this.$refs.listButtonImport.clearFile()
      })
    },
    // 导出
    export() {
      const params = Object.assign({}, this.queryCondition, { filename: '组织机构.xlsx' })
      this.api.export(params)
    },
    // 编辑窗口执行了保存操作，刷新数据
    handleDetailSave() {
      if (!this.isRefreshTree) {
        this.getTableData()
      } else {
        this.getTreeAndTableData()
      }
    },

    // 高级查询相关操作
    // 高级查询
    showAdvanceQuery() {
      this.api.initAdvanceQuery()
        .then(res => {
          this.$refs.advanceQueryScheme.show(this.entityCode, res.data)
        })
    },
    doAdvanceQuery(condition) {
      const params = Object.assign({ advanceQueryCondition: condition }, this.pageInfo, this.sortInfo)
      this.api.advanceQueryPage(params)
        .then(res => {
          this.tableData = res.data.records
          this.pageTotal = res.data.total
        }).finally(() => {
          this.currentId = this.constant.NO_ITEM_SELECTED
          this.loading = false
        })
    }

  },
  provide() {
    return {
      refresh: this.refresh,
      add: this.add,
      remove: this.remove,
      modify: this.modify,
      query: this.query,
      advanceQuery: this.showAdvanceQuery,
      enable: this.enable,
      disable: this.disable,
      view: this.view,
      batchRemove: this.batchRemove,
      downloadImportTemplate: this.downloadImportTemplate,
      import: this.import,
      export: this.export,
      handlePage: this.handlePage,
      handlePageSize: this.handlePageSize

    }
  }

}
